package com.google.hackathon.game.shriko.core;

import static playn.core.PlayN.assetManager;
import static playn.core.PlayN.graphics;
import playn.core.Image;
import playn.core.ImageLayer;

public class Ninja {
  public static final int INITIAL_X = 240;
  public static final int INITIAL_Y = 600;
  public static final int moveSpeed = 6;

  float x = INITIAL_X;
  float y = INITIAL_Y;
  float moveToX = INITIAL_X;
  float moveToY = INITIAL_Y;
  Image backImage = null;
  Image throwImage = null;
  Image leftImage = null;
  Image rightImage = null;
  ImageLayer layer = null;
  float throwing = 0;

  public void init() {
    backImage = assetManager().getImage("images/ninja_back.png");
    throwImage = assetManager().getImage("images/ninja_back02.png");
    leftImage = assetManager().getImage("images/ninja_left.png");
    rightImage = assetManager().getImage("images/ninja_right.png");
    layer = graphics().createImageLayer(backImage);
    graphics().rootLayer().add(layer);
    layer.setTranslation(x, y);
  }

  public void update(float delta) {
    int w;
    int h;
    if (throwing > 0) {
      throwing -= delta;
      layer.setImage(throwImage);
      w = throwImage.width();
      h = throwImage.height();
    } else if (x < moveToX) {
      x += moveSpeed;
      layer.setImage(rightImage);
      w = rightImage.width();
      h = rightImage.height();
    } else if (x > moveToX) {
      x -= moveSpeed;
      layer.setImage(leftImage);
      w = leftImage.width();
      h = leftImage.height();
    } else {
      layer.setImage(backImage);
      w = backImage.width();
      h = backImage.height();
    }
    if (Math.abs(x - moveToX) < moveSpeed) {
      x = moveToX;
    }

    // FIXME
    layer.setTranslation(x - w / 2, y - h / 2);
  }
}
